package com.foryou.swkj.core.util;






import com.foryou.swkj.core.exception.SystemException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;


/**
 * 系统配置文件(.properties)处理类 <BR>
 * 未加入缓存,因为读取不会很频繁
 *
 *
 */
public class SystemConfig {
    private static final String DEFAULT_CONFIG = "applications.properties";

    private static ClassLoader defaultClassLoader;

    private SystemConfig() {
    }

    /**
     * Returns the default classloader (may be null).
     *
     * @return The default classloader
     */
    public static ClassLoader getDefaultClassLoader() {
        return defaultClassLoader;
    }

    /**
     * Sets the default classloader
     *
     * @param defaultClassLoader
     *            - the new default ClassLoader
     */
    public static void setDefaultClassLoader(ClassLoader defaultClassLoader) {
        SystemConfig.defaultClassLoader = defaultClassLoader;
    }

    /**
     * Returns a resource on the classpath as a Properties object
     *
     * @param resource
     *            The resource to find
     * @return The resource
     * @throws IOException
     *             If the resource cannot be found or read
     */
    public static Properties getResourceAsProperties(String resource)
            throws SystemException {
        Properties props = new Properties();
        InputStream in = null;
        // 修复Unreleased Resource: Streams
        try {
            String propfile = resource;
            in = getResourceAsStream(propfile);
            props.load(in);
        } catch (IOException e) {
            throw new SystemException(e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    throw new SystemException(e);
                }
            }
        }
        return props;
    }

    public static Properties getResourceAsPropertiesByFile(String resource)
            throws SystemException {
        Properties props = new Properties();
        InputStream in = null;
        try {
            String propfile = resource;
            File propFile = new File(propfile);
            if (!propFile.exists()) {
                throw new SystemException("Could not find resource " + propFile);
            }
            in = new FileInputStream(propFile);
            props.load(in);
        } catch (IOException e) {
            throw new SystemException(e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    throw new SystemException(e);
                }
            }
        }
        return props;
    }

    /**
     * Returns a resource on the classpath as a Properties object
     *
     * @param loader
     *            The classloader used to load the resource
     * @param resource
     *            The resource to find
     * @return The resource
     * @throws IOException
     *             If the resource cannot be found or read
     */
    public static Properties getResourceAsProperties(ClassLoader loader,
                                                     String resource) throws SystemException {
        Properties props = new Properties();
        InputStream in = null;
        // 修复Unreleased Resource: Streams
        try {
            String propfile = resource;
            in = getResourceAsStream(loader, propfile);
            props.load(in);
        } catch (IOException e) {
            throw new SystemException(e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    throw new SystemException(e);
                }
            }
        }
        return props;
    }

    /**
     * Returns a resource on the classpath as a Stream object
     *
     * @param resource
     *            The resource to find
     * @return The resource
     * @throws IOException
     *             If the resource cannot be found or read
     */
    public static InputStream getResourceAsStream(String resource)
            throws SystemException {
        return getResourceAsStream(getClassLoader(), resource);
    }

    /**
     * Returns a resource on the classpath as a Stream object
     *
     * @param loader
     *            The classloader used to load the resource
     * @param resource
     *            The resource to find
     * @return The resource
     * @throws IOException
     *             If the resource cannot be found or read
     */
    public static InputStream getResourceAsStream(ClassLoader loader,
                                                  String resource) throws SystemException {
        InputStream in = null;
        if (loader != null)
            in = loader.getResourceAsStream(resource);
        if (in == null)
            in = ClassLoader.getSystemResourceAsStream(resource);
        if (in == null)
            throw new SystemException("Could not find resource " + resource);
        return in;
    }

    private static ClassLoader getClassLoader() {
        if (defaultClassLoader != null) {
            return defaultClassLoader;
        } else {
            return Thread.currentThread().getContextClassLoader();
        }
    }

    // 本行以上代码拷贝自com.ibatis.common.resources.Resources

    /**
     * 从默认配置文件(Application.properties)中读取配置
     *
     * @param key
     *            键
     * @param defaultValue
     *            默认值
     * @return
     */
    public static String getDefaultConfig(String key, String defaultValue)
            throws SystemException {
        return loadProperties(DEFAULT_CONFIG).getProperty(key, defaultValue);
    }

    public static String getDefaultConfig(String key) throws SystemException {
        return loadProperties(DEFAULT_CONFIG).getProperty(key);
    }

    public static String getConfig(String resource, String key)
            throws SystemException {
        return loadProperties(resource).getProperty(key);
    }

    public static String getConfig(String resource, String key,
                                   String defaultValue) throws SystemException {
        return loadProperties(resource).getProperty(key);
    }

    public static Properties loadProperties(String resource)
            throws SystemException {
        try {
            Properties props = getResourceAsProperties(resource);
            if (props != null) {
                return props;
            }
            throw new SystemException("Resource " + resource + " is not exist.");
        } catch (SystemException e) {
            throw new SystemException(e);
        }
    }
}
